package cn.tedu.service;

import cn.tedu.pojo.Car;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Component
public class TestJDBC {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        Connection conn = DriverManager.getConnection(url,"root","root");

        String sql = "select * from car where name=?";
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1,"bmw");
        //解析结果集
        ResultSet rs = ps.executeQuery();
        Car c = new Car();
        while (rs.next()){
            String name = rs.getString(1);
            String color = rs.getString(2);
            Double price = rs.getDouble(3);

            c.setName(name);
            c.setColor(color);
            c.setPrice(price);

        }
        System.out.println(c);
        rs.close();
        ps.close();
        conn.close();
    }
}
